// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_CHROME_CLEANER_HTTP_INTERNET_HELPERS_H_
#define CHROME_CHROME_CLEANER_HTTP_INTERNET_HELPERS_H_

#include <Windows.h>  // NOLINT

#include <stdint.h>

#include <map>
#include <string>

namespace chrome_cleaner {

// Parses the value of a Content-Type header.
// @param content_type_str The value of a Content-Type HTTP header (without the
//     label or ':').
// @param mime_type Receives the specified mime-type, if any, in lower-case.
//     Unmodified otherwise.
// @param charset Receives the specified charset, if any, in lower-case.
//     Unmodified otherwise.
// @param boundary Optional. Receives the (quoted) value of the boundary
//     parameter, if any. Unmodified otherwise.
void ParseContentType(const std::wstring& content_type_str,
                      std::wstring* mime_type,
                      std::wstring* charset,
                      bool* had_charset,
                      std::wstring* boundary);

// Parses an URL.
// @param url The URL to parse.
// @param scheme Receives the parsed scheme.
// @param host Receives the parsed host.
// @param port Receives the parsed port (or the implicit default port).
// @param path Receives the parsed path.
// @returns true if the URL is successfully parsed.
bool DecomposeUrl(const std::wstring& url,
                  std::wstring* scheme,
                  std::wstring* host,
                  uint16_t* port,
                  std::wstring* path);

// Composes an HTTP or HTTPS URL.
// @param host The URL host component.
// @param port The URL port component.
// @param path The URL path component.
// @returns The composed URL.
std::wstring ComposeUrl(const std::wstring& host,
                        uint16_t port,
                        const std::wstring& path,
                        bool secure);

// @returns A random string to be used as a multipart MIME message boundary.
std::wstring GenerateMultipartHttpRequestBoundary();

// Generates an appropriate Content-Type header (starting with "Content-Type:")
// for a multi-part HTTP message.
// @param boundary The MIME boundary to use.
// @returns An HTTP Content-Type header suitable for the multipart message
//     generated by GenerateMultipartHttpRequestBody.
std::wstring GenerateMultipartHttpRequestContentTypeHeader(
    const std::wstring boundary);

// Generates a multipart HTTP message body.
// @param parameters HTTP request parameters to be encoded in the body.
// @param upload_file File contents to be encoded in the body.
// @param file_part_name The parameter name to be assigned to the file part.
// @param boundary The MIME boundary to use.
// @returns A multipart HTTP message body.
std::string GenerateMultipartHttpRequestBody(
    const std::map<std::wstring, std::wstring>& parameters,
    const std::string& upload_file,
    const std::wstring& file_part_name,
    const std::wstring& boundary);

}  // namespace chrome_cleaner

#endif  // CHROME_CHROME_CLEANER_HTTP_INTERNET_HELPERS_H_
